package com.winning.pmph.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.winning.pmph.entity.KnowledgeField;
import com.winning.pmph.entity.ReleasedKnowledge;
import com.winning.pmph.entity.ReleasedKnowledgeField;
import com.winning.pmph.mapper.ReleasedKnowledgeMapper;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Component
@Transactional
public class ReleasedKnowledgeService extends ServiceImpl<ReleasedKnowledgeMapper, ReleasedKnowledge> {
    @Resource
    private ReleasedKnowledgeFieldService releasedKnowledgeFieldService;

    public void saveBeanAndField(ReleasedKnowledge knowledge) {
        saveOrUpdate(knowledge);
        // 更新原字段;
        releasedKnowledgeFieldService.remove(Wrappers.lambdaQuery(ReleasedKnowledgeField.class)
                .eq(KnowledgeField::getKnowledgeId, knowledge.getId())
                .eq(ReleasedKnowledgeField::getHidden, 0));
        releasedKnowledgeFieldService.saveBatch(knowledge.getReleasedKnowledgeFieldList());
    }
}
